package com.ruoyi.edh.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.edh.domain.EdhCoupon;
import com.ruoyi.edh.domain.EdhCouponLog;
import com.ruoyi.edh.mapper.EdhCouponLogMapper;
import com.ruoyi.edh.mapper.EdhCouponMapper;
import com.ruoyi.edh.service.IEdhCouponLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;


/**
 * 易订货-优惠券记录Service业务层处理
 * 
 * @author xxx
 * @date 2020-09-02
 */
@Service
public class EdhCouponLogServiceImpl implements IEdhCouponLogService
{
    @Autowired
    private EdhCouponLogMapper edhCouponLogMapper;

    @Autowired
    private EdhCouponMapper edhCouponMapper;

    /**
     * 查询易订货-优惠券记录
     * 
     * @param id 易订货-优惠券记录ID
     * @return 易订货-优惠券记录
     */
    @Override
    public EdhCouponLog selectEdhCouponLogById(Long id)
    {
        return edhCouponLogMapper.selectEdhCouponLogById(id);
    }

    /**
     * 查询易订货-优惠券记录列表
     * 
     * @param edhCouponLog 易订货-优惠券记录
     * @return 易订货-优惠券记录
     */
    @Override
    public List<EdhCouponLog> selectEdhCouponLogList(EdhCouponLog edhCouponLog)
    {
        return edhCouponLogMapper.selectEdhCouponLogList(edhCouponLog);
    }

    /**
     * 我得优惠券列表
     */
    @Override
    public HashMap<String, Object> myCouponList() {

        Long userId = SecurityUtils.getUserId();
         //未过期列表
        List<HashMap<String,Object>> normalList =  edhCouponLogMapper.normalList(userId);

         //已过期列表
        List<HashMap<String,Object>> expireList = edhCouponLogMapper.expireList(userId);
        return new HashMap<String,Object>(){
            {
                put("normalList",normalList);
                put("expireList",expireList);
            }
        };
    }

    /**
     * 新增易订货-优惠券记录
     * 
     * @param edhCouponLog 易订货-优惠券记录
     * @return 结果
     */
    @Override
    public int insertEdhCouponLog(EdhCouponLog edhCouponLog)
    {
        // 查询用户优惠券的领取上限
        EdhCoupon edhCoupon = edhCouponMapper.selectEdhCouponById(edhCouponLog.getCouponId());
        if (0 !=  edhCoupon.getLimitNum() ){
            EdhCouponLog couponLog = new EdhCouponLog();
            couponLog.setCreateBy(SecurityUtils.getUserId().toString());
            couponLog.setCouponId(edhCouponLog.getCouponId());
            List<EdhCouponLog> edhCouponLogs = edhCouponLogMapper.selectEdhCouponLogList(couponLog);
            if (null != edhCouponLogs && edhCouponLogs.size() >= edhCoupon.getLimitNum()){
                throw  new CustomException("您已达到优惠券领取上限,不可重复领取");
            }
        }
        if ( null !=  edhCoupon.getSendStartTime() && DateUtils.getNowDate().before(edhCoupon.getSendStartTime())){
            throw  new CustomException("领取失败,还未到优惠券领取时间");
        }
        if ( null !=  edhCoupon.getSendEndTime() && DateUtils.getNowDate().after(edhCoupon.getSendEndTime())){
            throw  new CustomException("领取失败,活动已结束");
        }
        // 设置优惠券的有效时间
        switch (edhCoupon.getEffectiveType()){
            // 如果是固定时间生效的话,则直接使用优惠券的生效时间
            case "1" :
                edhCouponLog.setUseStartTime(edhCoupon.getUseStartTime());
                edhCouponLog.setUseEndTime(edhCoupon.getUseEndTime());
                break;
            // 如果是自领取日生效的话则从领取日开始算时间
            case "2":
                Date now = DateUtils.getNowDate();
                edhCouponLog.setUseStartTime(now);
                edhCouponLog.setUseEndTime(DateUtils.getLater(now,edhCoupon.getEffectiveDays(), Calendar.DATE));
                break;
            default:
                throw new CustomException("领取失败,请联系客服设置好优惠券的生效时间");
        }
        edhCouponLog.setUserId(SecurityUtils.getUserId());
        edhCouponLog.setCreateTime(DateUtils.getNowDate());
        return edhCouponLogMapper.insertEdhCouponLog(edhCouponLog);
    }

    /**
     * 修改易订货-优惠券记录
     * 
     * @param edhCouponLog 易订货-优惠券记录
     * @return 结果
     */
    @Override
    public int updateEdhCouponLog(EdhCouponLog edhCouponLog)
    {
        return edhCouponLogMapper.updateEdhCouponLog(edhCouponLog);
    }

    /**
     * 批量删除易订货-优惠券记录
     * 
     * @param ids 需要删除的易订货-优惠券记录ID
     * @return 结果
     */
    @Override
    public int deleteEdhCouponLogByIds(Long[] ids)
    {
        return edhCouponLogMapper.deleteEdhCouponLogByIds(ids);
    }

    /**
     * 删除易订货-优惠券记录信息
     * 
     * @param id 易订货-优惠券记录ID
     * @return 结果
     */
    @Override
    public int deleteEdhCouponLogById(Long id)
    {
        return edhCouponLogMapper.deleteEdhCouponLogById(id);
    }

    /**
     *
     *
     * @description: 查询未使用的优惠卷
     * @param
     * @return: List<EdhCouponLog>
     * @author: 梁东宸
     * @time: 2020/9/18 1:45 下午
     */
    @Override
    public List<EdhCouponLog> queryNotExpiredUserCoupon() {
        return edhCouponLogMapper.queryNotExpiredUserCoupon();
    }
    /**
     *
     *
     * @description:  根据id修改已失效的优惠卷
     * @param id
     * @return: Integer
     * @author: 梁东宸
     * @time: 2020/9/18 1:51 下午
     */
    @Override
    @Async
    public Integer updateExpiredUserCoupon(String id) {
        return edhCouponLogMapper.updateExpiredUserCoupon(id);
    }
}
